#include "welcomeanimation.h"
#include <QtGui>

#include "desktop.h"
#include "desktopresource.h"
#include "scene.h"
#include "picturethread.h"
#include "ktv/model/resource/resource.h"

WelcomeAnimation::WelcomeAnimation(int w, int h)
    : width(w), height(h), indexForAnimation(0), animation(0)
{
    setFlag(QGraphicsItem::ItemIsFocusable);
    desktopResource = DesktopResource::getInstance();
    resource = Resource::getInstance();
    backgroundPixmap = desktopResource->backgroundPixmap;

    welcomeList = desktopResource->welcome;
    teamList = desktopResource->team;
}

void WelcomeAnimation::welcome()
{
    thread1 = new PictureThread(desktopResource->team, desktopResource->teamPath,
                  desktopResource->teamPrefix, desktopResource->teamSuffix,
                  desktopResource->teamCount, desktopResource->welcomeTime);
    thread1->start();

    currentList = welcomeList;
    qDebug() << "animation: \tstart welcome animation";
    animation = startAnimation(desktopResource->welcomeTime);
    connect(animation, SIGNAL(finished()), this, SLOT(team()));
}

void WelcomeAnimation::team()
{
    desktopResource->releaseWelcome();

    thread1 = new PictureThread(resource->welcome, resource->welcomePath,
                               resource->welcomePrefix, resource->welcomeSuffix,
                               resource->welcomeCount, resource->welcomeTime);
    thread1->start();

    currentList = teamList;
    qDebug() << "animation: \tstart team animation";
    animation = startAnimation(desktopResource->teamTime);
    connect(animation, SIGNAL(finished()), this, SLOT(finishAnimation()));
}

void WelcomeAnimation::finishAnimation()
{
    delete thread1;
    desktopResource->releaseTeam();
    static_cast<Scene *>(this->scene())->launch();
}

QPropertyAnimation *WelcomeAnimation::startAnimation(int duration)
{
    QPropertyAnimation *animation = new QPropertyAnimation(this, "index");
    animation->setDuration(duration);
    animation->setStartValue(0);
    animation->setEndValue(currentList->size() - 1);
    animation->start();
    return animation;
}

void WelcomeAnimation::setIndex(int index)
{
    indexForAnimation = index;

    backgroundPixmap = currentList->at(index);
    update();
}

void WelcomeAnimation::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Up:
        up();
        break;
    case Qt::Key_Down:
        down();
        break;
    case Qt::Key_Left:
        left();
        break;
    case Qt::Key_Right:
        right();
        break;
    }
}

QRectF WelcomeAnimation::boundingRect() const
{
    return QRectF(-width/2.0, -height/2.0, width, height);
}

void WelcomeAnimation::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
    painter->drawPixmap(-width/2.0, -height/2.0, width, height, *backgroundPixmap);
}
